<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>DataType</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>DataType</h1>

    
        <a href="#toc" class="jump">Jump to Table of Contents</a>
    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><div class="intro component">
    <p>
        The DataType Utility provides type-conversion and string-formatting
        convenience methods for numbers, dates, and XML documents.
    </p>
    
    <p>
        <strong>Note:</strong> In evolving the internationalization
        infrastructure, we have deprecated <code>config.locale</code>,
        <code>config.dateFormat</code>, and <code>DataType.Date.Locale</code>.
        These properties will be removed beginning with the 3.5.0 release.
    </p>
</div>
				
<h2 id="getting-started">Getting Started</h2>

<p>
To include the source files for DataType and its dependencies, first load
the YUI seed file if you haven't already loaded it.
</p>

<pre class="code prettyprint">&lt;script src=&quot;http:&#x2F;&#x2F;yui.yahooapis.com&#x2F;3.4.1&#x2F;build&#x2F;yui&#x2F;yui-min.js&quot;&gt;&lt;&#x2F;script&gt;</pre>


<p>
Next, create a new YUI instance for your application and populate it with the
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
YUI will automatically load any dependencies required by the modules you
specify.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Create a new YUI instance and populate it with the required modules.
YUI().use(&#x27;datatype&#x27;, function (Y) {
    &#x2F;&#x2F; DataType is available and ready for use. Add implementation
    &#x2F;&#x2F; code here.
});</pre>


<p>
For more information on creating YUI instances and on the
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
documentation for the <a href="../yui/index.html">YUI Global object</a>.
</p>

				
<h2 id="using">Using the DataType utility</h2>
			
<h3 id="dates">Dates</h3>

<h4 id="formattingdates">Formatting dates</h4>

<p>
    <code>DataType.Date.format()</code> will output dates as strings formatted
    using <code>strftime</code> tokens. Some of the formats are localizable, so
    be sure to specify the <code>lang</code> property of the YUI instance's
    config object.
</p>

<pre class="code prettyprint">YUI({lang:&quot;ko-KR&quot;}).use(&quot;datatype-date&quot;, function(Y) {
    &#x2F;&#x2F; display the current date and time in localized format
    alert(Y.DataType.Date.format(new Date(), {format:&quot;%x %X&quot;}));
});</pre>


<p>
    <a name="addDateFormat"></a>The module has support for a large number of
    languages built in.  If you need a language that's not supported, you can
    register the necessary localized data yourself using facilities of the <a
    href="../intl">Internationalization utility</a>.  The resource bundle you
    provide needs to have properties corresponding to the locale-sensitive <a
    href="http://yuilibrary.com/yui/docs/api/classes/DataType.Date.html#method_format"> strftime format
    specifiers</a>:
</p>
                
<ul>
    <li><b>a:</b> list of abbreviated weekday names, from Sunday to Saturday
    <li><b>A:</b> list of full weekday names, from Sunday to Saturday
    <li><b>b:</b> list of abbreviated month names, from January to December
    <li><b>B:</b> list of full month names, from January to December
    <li><b>c:</b> preferred date and time representation
    <li><b>p:</b> list of strings corresponding to "AM" and "PM"
    <li><b>P:</b> same as "p", but lower case
    <li><b>x:</b> preferred date representation without the time
    <li><b>X:</b> preferred time representation without the date
</ul>

<pre class="code prettyprint">YUI().use(&quot;intl&quot;, &quot;datatype-date-format&quot;, function(Y) {
    &#x2F;&#x2F; provide data for Punjabi in India
    Y.Intl.add(&quot;datatype-date-format&quot;, &quot;pa-IN&quot;, {
        &quot;a&quot;:[&quot;ਐਤ.&quot;,&quot;ਸੋਮ.&quot;,&quot;ਮੰਗਲ.&quot;,&quot;ਬੁਧ.&quot;,&quot;ਵੀਰ.&quot;,&quot;ਸ਼ੁਕਰ.&quot;,&quot;ਸ਼ਨੀ.&quot;],
        &quot;A&quot;:[&quot;ਐਤਵਾਰ&quot;,&quot;ਸੋਮਵਾਰ&quot;,&quot;ਮੰਗਲਵਾਰ&quot;,&quot;ਬੁਧਵਾਰ&quot;,&quot;ਵੀਰਵਾਰ&quot;,&quot;ਸ਼ੁੱਕਰਵਾਰ&quot;,&quot;ਸ਼ਨੀਚਰਵਾਰ&quot;],
        &quot;b&quot;:[&quot;ਜਨਵਰੀ&quot;,&quot;ਫ਼ਰਵਰੀ&quot;,&quot;ਮਾਰਚ&quot;,&quot;ਅਪ੍ਰੈਲ&quot;,&quot;ਮਈ&quot;,&quot;ਜੂਨ&quot;,&quot;ਜੁਲਾਈ&quot;,&quot;ਅਗਸਤ&quot;,&quot;ਸਤੰਬਰ&quot;,&quot;ਅਕਤੂਬਰ&quot;,&quot;ਨਵੰਬਰ&quot;,&quot;ਦਸੰਬਰ&quot;],
        &quot;B&quot;:[&quot;ਜਨਵਰੀ&quot;,&quot;ਫ਼ਰਵਰੀ&quot;,&quot;ਮਾਰਚ&quot;,&quot;ਅਪ੍ਰੈਲ&quot;,&quot;ਮਈ&quot;,&quot;ਜੂਨ&quot;,&quot;ਜੁਲਾਈ&quot;,&quot;ਅਗਸਤ&quot;,&quot;ਸਤੰਬਰ&quot;,&quot;ਅਕਤੂਬਰ&quot;,&quot;ਨਵੰਬਰ&quot;,&quot;ਦਸੰਬਰ&quot;],
        &quot;c&quot;:&quot;%a, %Y %b %d %l:%M:%S %p %Z&quot;,
        &quot;p&quot;:[&quot;ਸਵੇਰੇ&quot;,&quot;ਸ਼ਾਮ&quot;],
        &quot;P&quot;:[&quot;ਸਵੇਰੇ&quot;,&quot;ਸ਼ਾਮ&quot;],
        &quot;x&quot;:&quot;%d&#x2F;%m&#x2F;%Y&quot;,
        &quot;X&quot;:&quot;%l:%M:%S %p&quot;
    });
    &#x2F;&#x2F; switch to Punjabi
    Y.Intl.setLang(&quot;datatype-date-format&quot;, &quot;pa-IN&quot;);
    &#x2F;&#x2F; now dates are formatted in Punjabi
    alert(Y.DataType.Date.format(new Date(), {format:&quot;%A %x %X&quot;}));
});</pre>


<h4 id="parsingdates">Parsing dates</h4>

<p>
    <code>DataType.Date.parse()</code> accepts any value supported by the
    JavaScript <code>Date()</code> constructor and converts it to a Date
    object. Invalid values will return null.
</p>

<pre class="code prettyprint">var date1 = Y.DataType.Date.parse(&quot;December 17, 1995 03:24:00&quot;);

var date2 = Y.DataType.Date.parse(1995,11,17);

var date3 = Y.DataType.Date.parse(1995,11,17,3,24,0);

var date4 = Y.DataType.Date.parse(948548583);</pre>


<h3 id="numbers">Numbers</h3>

<h4 id="formattingnumbers">Formatting numbers</h4>

<p>
    JavaScript Number values can be formatted with a variety options into
    string values using <code>DataType.Number.format()</code>.
</p>

<pre class="code prettyprint">alert(Y.DataType.Number.format(123123123.176,{
    prefix: &quot;€&quot;,
    thousandsSeparator: &quot;.&quot;,
    decimalSeparator: &quot;,&quot;,
    decimalPlaces: 2,
    suffix: &quot; (EUR)&quot;
}));</pre>


<h4 id="parsingnumbers">Parsing numbers</h4>

<p>
    String values can be converted into Number objects with
    <code>DataType.Number.parse()</code>.
</p>

<pre class="code prettyprint">var number = Y.DataType.Number.parse(&quot;123123&quot;);</pre>


<h3 id="xml">XML</h3>

<h4 id="formattingxml">Formatting XML</h4>

<p>
    <code>DataType.XML.format()</code> will accept an XML document and return
    its string representation. Note that browsers may slightly differ in the
    exact string that is returned.
</p>

<pre class="code prettyprint">var myString = Y.DataType.XML.format(myXMLDocument);</pre>


<h4 id="parsingxml">Parsing XML</h4>

<p>
    <code>DataType.XML.parse()</code> will accept a string representation of
    XML and return an XML document object. Note that browsers differ in their
    handling of invalid syntax but will in general return an XML document even
    under error conditions.
</p>

<pre class="code prettyprint">var myXMLDocument = Y.DataType.XML.parse(&quot;&lt;myroot&gt;&lt;item type=&#x27;foo&#x27;&gt;&lt;name&gt;Abc&lt;&#x2F;name&gt;&lt;rank&gt;1&lt;&#x2F;rank&gt;&lt;&#x2F;item&gt;&lt;item type=&#x27;bar&#x27;&gt;&lt;name&gt;Def&lt;&#x2F;name&gt;&lt;rank&gt;2&lt;&#x2F;rank&gt;&lt;&#x2F;item&gt;&lt;item type=&#x27;bat&#x27;&gt;&lt;name&gt;Ghhi&lt;&#x2F;name&gt;&lt;rank&gt;3&lt;&#x2F;rank&gt;&lt;&#x2F;item&gt;&lt;&#x2F;myroot&gt;&quot;);</pre>


<h3 id="parsers">Y.Parsers shortcuts</h3>

<p>
    The functions <code>DataType.Date.parse()</code> and
    <code>DataType.Number.parse()</code> are registered with the
    <code>Parsers</code> object for seamless integration with the DataSchema
    Utility. For dynamic type enforcing when data is being normalized against a
    schema, simply point to the appropriate function using the built-in
    shortcut in your schema definition. Parsing your data in this manner is
    essential if your numerical or date data comes over the wire as JSON, since
    all the values will be strings.
</p>

<pre class="code prettyprint">YUI().use(&quot;datatype&quot;, &quot;dataschema&quot;, function(Y) {
    var data_in = {
            &quot;results&quot;:[
                {&quot;string&quot;:&quot;aardvark&quot;, &quot;number&quot;:&quot;1&quot;, &quot;date&quot;:&quot;Jan 1, 2001&quot;},
                {&quot;string&quot;:&quot;bat&quot;, &quot;number&quot;:&quot;2&quot;, &quot;date&quot;:&quot;Feb 2, 2002&quot;},
                {&quot;string&quot;:&quot;camel&quot;, &quot;number&quot;:&quot;3&quot;, &quot;date&quot;:&quot;March 3, 2003&quot;}
            ]
        },
        schema = {
            resultListLocator: &quot;results&quot;,
            resultFields: [
                &quot;string&quot;, &#x2F;&#x2F; needs no parsing
                {key:&quot;number&quot;, parser: &quot;number&quot;}, &#x2F;&#x2F; converts string values to numbers
                {key:&quot;date&quot;, parser: &quot;date&quot;}] &#x2F;&#x2F; converts string values to dates
        },
        data_out = Y.DataSchema.JSON.apply(schema, data_in);
});</pre>

</div>
        </div>

        <div id="sidebar" class="yui3-u">
            
                <div id="toc" class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Table of Contents</h2>
                    </div>

                    <div class="bd">
                        <ul class="toc">
<li>
<a href="#getting-started">Getting Started</a>
</li>
<li>
<a href="#using">Using the DataType utility</a>
<ul class="toc">
<li>
<a href="#dates">Dates</a>
<ul class="toc">
<li>
<a href="#formattingdates">Formatting dates</a>
</li>
<li>
<a href="#parsingdates">Parsing dates</a>
</li>
</ul>
</li>
<li>
<a href="#numbers">Numbers</a>
<ul class="toc">
<li>
<a href="#formattingnumbers">Formatting numbers</a>
</li>
<li>
<a href="#parsingnumbers">Parsing numbers</a>
</li>
</ul>
</li>
<li>
<a href="#xml">XML</a>
<ul class="toc">
<li>
<a href="#formattingxml">Formatting XML</a>
</li>
<li>
<a href="#parsingxml">Parsing XML</a>
</li>
</ul>
</li>
<li>
<a href="#parsers">Y.Parsers shortcuts</a>
</li>
</ul>
</li>
</ul>
                    </div>
                </div>
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="Formatting dates into strings.">
                                        <a href="datatype-dateformat.html">DataType.Date.format()</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Formatting dates into strings using pre-packaged language resource bundles.">
                                        <a href="datatype-dateformat-lang.html">Formatting Dates Using Language Resource Bundles</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Parsing data into dates.">
                                        <a href="datatype-dateparse.html">DataType.Date.parse()</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Formatting numbers into strings.">
                                        <a href="datatype-numberformat.html">DataType.Number.format()</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Parsing data into numbers.">
                                        <a href="datatype-numberparse.html">DataType.Number.parse()</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Formatting XML documents into strings.">
                                        <a href="datatype-xmlformat.html">DataType.XML.format()</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Parsing strings into XML documents.">
                                        <a href="datatype-xmlparse.html">DataType.XML.parse()</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            

            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
